/*
 * Copyright (c) 2020 WildFireChat. All rights reserved.
 */

package com.cincc.cinccimdemo.ui.main;

import android.text.Editable;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.Toast;

import com.cincc.cinccimdemo.R;
import com.cincc.cinccimdemo.common.AttrConstants;
import com.cincc.cinccimdemo.common.Constants;
import com.cincc.cinccimdemo.ui.IMBaseActivity;
import com.cincc.cinccimdemo.widget.SimpleTextWatcher;
import com.cincc.im.core.IMSipPhoneCtrl;


public class SetAliasActivity extends IMBaseActivity {

    EditText aliasEditText;

    private MenuItem menuItem;

    protected void bindViews() {
        super.bindViews();
        aliasEditText = findViewById(R.id.aliasEditText);
        aliasEditText.addTextChangedListener(new SimpleTextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {
                onAliasEditTextChange();
            }
        });
    }

    @Override
    protected int contentLayout() {
        return R.layout.contact_set_alias_activity;
    }

    String userId = "";
    String alias = "";

    @Override
    protected void afterViews() {
        userId = getIntent().getStringExtra(AttrConstants.USER_ID);
        alias = getIntent().getStringExtra(AttrConstants.ALIAS);

        String hint = TextUtils.isEmpty(userId) ? Constants.userInfo.nickName : alias;
        if (!TextUtils.isEmpty(hint)) {
            aliasEditText.setHint(hint);
        }
    }

    @Override
    protected int menu() {
        return R.menu.user_set_alias;
    }

    @Override
    protected void afterMenus(Menu menu) {
        menuItem = menu.findItem(R.id.save);
        menuItem.setEnabled(false);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.save) {
            changeAlias();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    void onAliasEditTextChange() {
        menuItem.setEnabled(aliasEditText.getText().toString().trim().length() > 0);
    }

    private void changeAlias() {
        String displayName = aliasEditText.getText().toString().trim();
        if (TextUtils.isEmpty(userId)) {
            IMSipPhoneCtrl.getInstance().setNickName(displayName, imResponse -> {
                String result = imResponse.getCode() == 0 ? "修改成功" : imResponse.getMsg();
                runOnUiThread(() -> {
                    Toast.makeText(SetAliasActivity.this, result, Toast.LENGTH_SHORT).show();
                    if (imResponse.getCode() == 0) {
                        Constants.userInfo.nickName = displayName;
                        finish();
                    }
                });
            });
        } else {
            IMSipPhoneCtrl.getInstance().updateUserAlias(userId, displayName, imResponse -> {
                String result = imResponse.getCode() == 0 ? "修改成功" : imResponse.getMsg();
                runOnUiThread(() -> {
                    Toast.makeText(SetAliasActivity.this, result, Toast.LENGTH_SHORT).show();
                    if (imResponse.getCode() == 0) {
                        finish();
                    }
                });
            });
        }
    }
}
